package Javaa.Base.Thread;
//create an example of using ConcurrentHashMap class in multithreaded environment

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
    public static void main(String[] args) {
        // Create a ConcurrentHashMap to store String keys and Integer values
        Map<String, Integer> ageMap = new ConcurrentHashMap<>();

        // Create multiple threads to add key-value pairs to the ConcurrentHashMap
        Thread writer1 = new Thread(() -> {
            ageMap.put("Bob", 25);
            ageMap.put("Alice", 30);
        });

        Thread writer2 = new Thread(() -> {
            ageMap.put("Alice", 28);
            ageMap.put("Charlie", 35);
            ageMap.put("Diana", 28);
      
        });

        // Start the writer threads
        writer1.start();
        writer2.start();

        // Wait for both threads to finish
        try {  
            writer2.join();
            writer1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Access and print all key-value pairs in the ConcurrentHashMap
        System.out.println("All entries in the age map:");
        for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}